<template>
    <div class="demo">
        <h2>{{component.__sourceCodeTitle}}</h2>
        <div class="demo-component">
            <component :is="component" />
        </div>
        <div class="demo-actions">
            <Button v-if="codeVisible" @click="codeVisible = false">隐藏代码</Button>
            <Button v-else @click="codeVisible = true">查看代码</Button>
        </div>
        <div class="demo-code" v-if="codeVisible">
            <pre class="language-html" v-html="html" />
        </div>
    </div>
</template>

<script lang="ts">

    import { ref, computed } from 'vue'

    import 'prismjs'
    import 'prismjs/themes/prism.css'

    import Button from '../lib/Button.vue'

    const Prism = (window as any).Prism;

    export default {
        components: { Button },
        props: {
            component: Object
        },
        setup(props) {
            const codeVisible = ref(false);
            const html = computed(() => {
                return Prism.highlight(props.component.__sourceCode, Prism.languages.html, 'html')
            });

            return { Prism, html, codeVisible }
        }
    }
</script>

<style lang="scss" scoped>
    $border-color: #d9d9d9;

    .demo {
        margin: 16px 0 32px;
        border: 1px solid $border-color;

        > h2 {
            padding: 8px 16px;
            font-size: 20px;
            border-bottom: 1px solid $border-color;
        }

        &-component {
            padding: 16px;
        }

        &-actions {
            padding: 8px 16px;
            border-top: 1px dashed $border-color;
        }

        &-code {
            padding: 16px;
            border-top: 1px dashed $border-color;

            > pre {
                margin: 0;
                line-height: 1.1;
                font-family: consolas, 'Courier New', Courier,Monospace;
            }
        }
    }
</style>